package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MesInspection;
import com.ruoyi.system.service.IMesInspectionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 质检管理Controller
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@RestController
@RequestMapping("/system/inspection")
public class MesInspectionController extends BaseController
{
    @Autowired
    private IMesInspectionService mesInspectionService;

    /**
     * 查询质检管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:inspection:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesInspection mesInspection)
    {
        startPage();
        List<MesInspection> list = mesInspectionService.selectMesInspectionList(mesInspection);
        return getDataTable(list);
    }

    /**
     * 导出质检管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:inspection:export')")
    @Log(title = "质检管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesInspection mesInspection)
    {
        List<MesInspection> list = mesInspectionService.selectMesInspectionList(mesInspection);
        ExcelUtil<MesInspection> util = new ExcelUtil<MesInspection>(MesInspection.class);
        util.exportExcel(response, list, "质检管理数据");
    }

    /**
     * 获取质检管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:inspection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(mesInspectionService.selectMesInspectionById(id));
    }

    /**
     * 新增质检管理
     */
    @PreAuthorize("@ss.hasPermi('system:inspection:add')")
    @Log(title = "质检管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesInspection mesInspection)
    {
        if (!mesInspectionService.isInspIdUnique(mesInspection,true))
        {
            return AjaxResult.error("质检内容编码重复");
        }
        return toAjax(mesInspectionService.insertMesInspection(mesInspection));
    }

    /**
     * 修改质检管理
     */
    @PreAuthorize("@ss.hasPermi('system:inspection:edit')")
    @Log(title = "质检管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesInspection mesInspection)
    {
        // 在添加质检方案前，先检查质检方案编码是否唯一
        if (!mesInspectionService.isInspIdUnique(mesInspection,false)) {
            return AjaxResult.error("质检内容编码重复");
        }
        return toAjax(mesInspectionService.updateMesInspection(mesInspection));
    }

    /**
     * 删除质检管理
     */
    @PreAuthorize("@ss.hasPermi('system:inspection:remove')")
    @Log(title = "质检管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mesInspectionService.deleteMesInspectionByIds(ids));
    }
}
